package com.zx.oa.dao.major;

import com.zx.oa.dao.BaseDao;
import com.zx.oa.entity.Major;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MajorDaoImpl extends BaseDao implements MajorDao{

    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;

    @Override
    public List<Major> getMajorAll() {
        List<Major> majorList=new ArrayList<>();
        conn=this.getConnection();
        String sql="SELECT id,major FROM ztemajor";
        try {
            pstmt=conn.prepareStatement(sql);
            rs=pstmt.executeQuery();
            while (rs.next()){
                Integer id=rs.getInt("id");
                String major=rs.getString("major");
                Major maj=new Major(id,major);
                majorList.add(maj);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return majorList;
    }
}
